package com.sz.sidi.oa.katable;

import java.awt.Robot;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Control;

import util.Session;

import com.sz.sidi.oa.Activator;
import com.sz.sidi.util.TimeHelper;

public class DoctorConfiguration {

	public static boolean dragZzys = false;// 用于主页面拖放事件 中拖放源 与目地的沟通
	public static String[] floatStrInputArray = { "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", ".", "。" };
	public static String[] intStrInputArray = { "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9" };
	public static final String DEFAULT_KH = "$DEFAULT_KH"; //$NON-NLS-1$
	public static final String DEFAULT_UERR_CODE = "$DEFAULT_UERR_CODE"; //$NON-NLS-1$
	public static final String DEFAULT_UERR_NAME = "$DEFAULT_UERR_NAME"; //$NON-NLS-1$
	public static final String MAIN_VIEW = "com.ztkj.his.zygl.zydoctor.views.MainView";

	// public static final Image manImg = RcpResourceManager.getPluginImage(
	//			Activator.getDefault(), "icons/man.bmp"); //$NON-NLS-1$
	// public static final Image womanImg = RcpResourceManager.getPluginImage(
	//			Activator.getDefault(), "icons/woman.bmp"); //$NON-NLS-1$
	// public static final Image bedImg = RcpResourceManager.getPluginImage(
	//			Activator.getDefault(), "icons/bed.bmp"); //$NON-NLS-1$

	public static final Color bgTempTable = new Color(null, 192, 192, 192);
	/**
	 * 所有字体
	 */
	public static final Font f8 = new Font(null, "宋体", 8,
			SWT.NORMAL);
	public static final Font f9 = new Font(null,"宋体", 9,
			SWT.NORMAL);
	public static final Font f9B = new Font(null,"宋体", 9, SWT.BOLD);
	public static final Font f10 = new Font(null,"宋体", 10,
			SWT.NORMAL);
	public static final Font f10B = new Font(null,"宋体", 10,
			SWT.BOLD);
	public static final Font f11 = new Font(null,"宋体", 11,
			SWT.NORMAL);
	public static final Font f11B = new Font(null,"宋体", 11,
			SWT.BOLD);
	public static final Font f12 = new Font(null,"宋体", 12,
			SWT.NORMAL);

	/**
	 * ExpandBar 样式
	 */
	public static final Font EXPAND_FONT = f10B;

	public static final Color EXPAND_FG_COLOR = new Color(null,0,
			0, 0);

	public static final Color EXPAND_BG_COLOR = new Color(null,
			240, 255, 241);

	/**
	 * Tabfolder 样式
	 */
	public static final Font TABFOLDER_FONT = f10;
	// public static final Color TABFOLDER_FG_COLOR =
	// new Color(null,
	// 40, 70, 143);
	//
	// public static final Color TABFOLDER_BG_COLOR =
	// new Color(null,
	// 240, 255, 241);
	//
	// public static final Color[] TABFOLDER_SELECTION_BG = new Color[] {
	// new Color(null,237, 245, 254),
	// new Color(null,179, 201, 253) };

	public static final Color TABFOLDER_FG_COLOR = new Color(null,
			0, 0, 0);

	public static final Color TABFOLDER_BG_COLOR = new Color(null,
			255, 255, 255);
	

	public static final Color[] TABFOLDER_SELECTION_BG = new Color[] {
			new Color(null,240, 250, 217),
			new Color(null,206, 230, 151) };

	public static final int[] TABFOLDER_SELECTION_PERCENTS = new int[] { 55 };

	/**
	 * Table 样式
	 */
	public static final Font TABLE_FONT = f11;

	// 表格的前景颜色
	public static final Color TABLE_FG_COLOR = new Color(null,0,
			0, 128);// new Color(null,67,95, 128);
	// new Color(null,85,102, 128);

	public static final Color TABLE_ROW_COLOR = new Color(null,
			235, 245, 235);// new Color(null,240, 243,
	// 248);//隔行换色的背景颜色

	public static final Color TABLE_MODIFY_COLOR = new Color(null,
			240, 243, 248);

	// 选中单元格的背景颜色
	public static final Color TABLE_EDIT_CONTROL = new Color(null,
			188, 221, 255);// new Color(null,0, 255, 255);
	// new Color(null,208, 241, 255);
	// 选中单元格的前景颜色
	public static final Color TABLE_EDIT_FoceCONTROL = new Color(null,0, 0, 0);
	// new Color(null,208, 241, 255);

	// 选中行的背景颜色1
	public static final Color TABLE_SELECTION_BG1 = new Color(null,68, 102, 149);
	// RcpResourceManager
	// .getColor(165, 186, 210);//new Color(null,15, 62, 125);
	// 选中行的背景颜色2
	public static final Color TABLE_SELECTION_BG2 = new Color(null,235, 235, 235);// new Color(null,133, 162,
	// 222);
	// new Color(null,179, 201, 253);
	// new Color(null,0, 0, 125);
	// new Color(null,237, 245, 254),

	public static final int ROW_HEIGHT = 27;

	/**
	 * ToolBar 样式
	 */
	public static final Font TOOLBAR_FONT = f11B;

	public static final Color TOOLBAR_FG_COLOR = new Color(null,
			40, 70, 143);

	public static final Color TOOLBAR_BG_COLOR = new Color(null,
			240, 255, 241);
 
 
 
	public static final Color DIALOG_STYLE = new Color(null,235,
			233, 237);

	// BedComposite中颜色设置

	public static final Color BACKGROUND_COLOUR = new Color(null,
			244, 244, 247);
	
	//是否开启时间锁   add by fangding
	public static final String CHECKTIME_KEY="$CHECKTIME_KEY";//是否开启时间锁 0:不开,1:开启
	public static final String CHECKTIME_DEFAULT_VALUE="true";//是否开启时间锁0:不开，1：开启
	
	
	/**
	 * 标识字体颜色
	 */
	public static String BS_FONTCOLOR_KEY = "$BS_FONTCOLOR_KEY"; //$NON-NLS-1$
	public static String CW_DEFAULT_COLOR = "0, 0, 0"; //$NON-NLS-1$
	public static String BRZT_DEFAULT_COLOR = "0, 128, 0"; //$NON-NLS-1$
	public static String XM_DEFAULT_COLOR = "255, 0, 0"; //$NON-NLS-1$
	public static String NL_DEFAULT_COLOR = "0,0,0"; //$NON-NLS-1$
	public static String ZYH_DEFAULT_COLOR = "255, 0, 0"; //$NON-NLS-1$
	public static String HL_DEFAULT_COLOR = "0, 0, 0"; //$NON-NLS-1$
	public static String FYLB_DEFAULT_COLOR = "0,0,0"; //$NON-NLS-1$
	public static String RQ_DEFAULT_COLOR = "0,0,0"; //$NON-NLS-1$
	public static String JBMC_DEFAULT_COLOR = "51,78,161";//"128, 128, 128"; //$NON-NLS-1$
	public static String BS_DEFAULT_COLOR = "51,78,161";//"128, 128, 128"; //$NON-NLS-1$
	public static String YS_DEFAULT_COLOR = "0,0,0";//"255,0,0";//$NON-NLS-1$
	private static String beginDate = "", endDate = "";
	public static String SYSNAV_KEY = "$SYSNAV_KEY";
	public static String SYSNAV_DEFAULT_VALUE = "false";
	public static String alarmSeparateMinutes_KEY= "$ALARMSEPARATEMINUTES_KEY";
	public static String alarmSeparateMinutes_DEFAULT_VALUE = "30";
	 
	
	public static String getEndDate() {
		return endDate;
	}

	public static void setEndDate(String endDate) {
		DoctorConfiguration.endDate = endDate;
	}

	public static String getBeginDate() {
		return beginDate;
	}

	public static void setBeginDate(String beginDate) {
		DoctorConfiguration.beginDate = beginDate;
	}

	/**
	 * 实现按回车键，从一个text切换到另一个text，类似于TAB键功能
	 * */
	public static void setEnterList(Control[] enterList) {
		if (enterList != null) {
			for (int i = 0; i < enterList.length; i++) {
				final Control control = enterList[i];
				enterList[i].addKeyListener(new KeyListener() {
					public void keyPressed(KeyEvent e) {
						// TODO Auto-generated method stub
						try {
							if (e.keyCode == SWT.CR
									|| e.keyCode == SWT.KEYPAD_CR) {
								Robot robot = new Robot();
								robot.keyPress(java.awt.event.KeyEvent.VK_TAB);
							}
							if (e.keyCode == SWT.ESC) {
								control.getShell().close();
							}
						} catch (Exception ex) {
							ex.printStackTrace();
						}
					}

					public void keyReleased(KeyEvent e) {
						// TODO Auto-generated method stub

					}

				});
			}
		}
	}

	/**
	 *比较日期的大小
	 **/
	public static int compareTo(String date) {
		String s = TimeHelper.dateToString(TimeHelper.StringToDate(date));
		String s1 = TimeHelper.getCurrentDateString();
		return s.compareTo(s1);
	}

	/**
	 * 获取对象的属性值
	 * 
	 * @param obj
	 *            对象
	 * @param fieldName
	 *            属性名
	 * @return
	 */
	public static Object getProperty(Object obj, String fieldName) {
		if (obj == null || fieldName == null)
			return "";

		Class cls = obj.getClass();

		Field fieldlist[] = cls.getDeclaredFields();

		Map<String, String> map = new HashMap<String, String>();
		for (int i = 0; i < fieldlist.length; i++) {
			Field fld = fieldlist[i];
			String fName = fld.getName();
			map.put(fName.toLowerCase(), fName);
		}

		Object temp = null;

		if (!map.containsKey(fieldName)) {
			return temp;
		}

		try {
			Field field = cls.getDeclaredField(fieldName);
			field.setAccessible(true);
			temp = field.get(obj);

			// if (field.getType().equals(java.util.Date.class) && temp != null
			// && !temp.equals("")) {
			// temp = TimeHelper.dateToString((Date) temp,
			// "yyyy-MM-dd HH:mm:ss");
			// }
		} catch (Exception e) {
			temp = "";
			Session.showExceptionMsg(e,"");
		}
		return temp;
	}

	/**
	 * 设置对象的属性值
	 * 
	 * @param obj
	 *            对象
	 * @param fieldName
	 *            属性名
	 * @param fieldValue
	 *            属性值
	 */
	public static void setProperty(Object obj, String fieldName,
			Object fieldValue) {
		if (obj == null || fieldName == null || fieldValue == null)
			return;

		Class cls = obj.getClass();

		Field fieldlist[] = cls.getDeclaredFields();

		Map<String, String> map = new HashMap<String, String>();
		for (int i = 0; i < fieldlist.length; i++) {
			Field fld = fieldlist[i];
			String fName = fld.getName();
			map.put(fName.toLowerCase(), fName);
		}

		if (!map.containsKey(fieldName)) {

		}

		try {
			Field field = cls.getDeclaredField(fieldName);
			field.setAccessible(true);
			Class fieldType = field.getType();

			String methodName = "set";

			methodName += fieldName.toUpperCase().substring(0, 1);
			methodName += fieldName.substring(1, fieldName.length());

			Method method = cls.getDeclaredMethod(methodName, fieldType);

			if (fieldType.equals(Boolean.class)
					|| fieldType.equals(boolean.class)) {
				method.invoke(obj, Boolean.parseBoolean(fieldValue.toString()));
			} else if (fieldType.equals(Byte.class)
					|| fieldType.equals(byte.class)) {
				method.invoke(obj, Byte.parseByte(fieldValue.toString()));
			} else if (fieldType.equals(Short.class)
					|| fieldType.equals(short.class)) {
				method.invoke(obj, Short.parseShort(fieldValue.toString()));
			} else if (fieldType.equals(Integer.class)
					|| fieldType.equals(int.class)) {
				method.invoke(obj, Integer.parseInt(fieldValue.toString()));
			} else if (fieldType.equals(Long.class)
					|| fieldType.equals(long.class)) {
				method.invoke(obj, Long.parseLong(fieldValue.toString()));
			} else if (fieldType.equals(Float.class)
					|| fieldType.equals(float.class)) {
				method.invoke(obj, Float.parseFloat(fieldValue.toString()));
			} else if (fieldType.equals(Double.class)
					|| fieldType.equals(double.class)) {
				method.invoke(obj, Double.parseDouble(fieldValue.toString()));
			} else if (fieldType.equals(Date.class)) {
				Date date = null;
				if (fieldValue != null) {
					String fieldValue1 = fieldValue.toString().trim();
					fieldValue1 = fieldValue.toString().trim()
							.replace("-", "/");
					fieldValue1 = fieldValue1.toString().trim().replace("—",
							"/");
					fieldValue1 = fieldValue1.toString().trim().replace(".",
							"/");
					date = TimeHelper.stringToTime(fieldValue1,
							"yyyy/MM/dd HH:mm:ss");
					if (date == null) {
						date = TimeHelper.stringToTime(fieldValue1,
								"yyyyMMdd HH:mm:ss");
						if (date == null) {
							try {
								date = (Date) fieldValue;
							} catch (Exception e) {
							}
						}
					}

				}
				method.invoke(obj, date);
			} else {
				method.invoke(obj, fieldType.cast(fieldValue));
			}

		} catch (Exception e) {
			// SuperContext.showExceptionMsg(e);
		}
	}

}
